$(document).ready(function(){
	$(".ajax-loader").hide();
	
	//实现搜索框中“证券搜索”的提示效果
	$("#search .textbox").focus(function(){
		if($(this).val() == "证券搜索"){
			$(this).css("color", "black").val("");
		}
	}).blur(function(){
		if($(this).val() == ""){
			$(this).css("color", "#949AAA").val("证券搜索");
		}
	});
	
	
	$(".press-entity").mouseover(function(){
		entity = $(this);
		$(".signboard", entity).css('opacity','0.6');
	}).mouseout(function(){
		entity = $(this);
		$(".signboard", entity).css('opacity','0.1');
	});
	
	//登录动作的表单提交
	var options = { 
	        //target:        '#output2',   // target element(s) to be updated with server response 
	        beforeSubmit:  validateAndLockLoginForm,  // pre-submit callback 
	        success:       processLoginResponse,  // post-submit callback
	        
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        timeout:   5000,
	        error: onLoginError
	}; 
	$("#login form").ajaxForm(options);
	
	function validateAndLockLoginForm(formData, jqForm, options){
		for( var i = 0; i < formData.length; i++){
			if(formData[i].name == "username"){
				if($.trim(formData[i].value) == ""){
					$("#login .err-msg").text("请输入您的用户名");
					return false;
				}
			}else if(formData[i].name == "password"){
				if(formData[i].value == ""){
					$("#login .err-msg").text("请输入密码");
					return false;
				}
			}
		}
		$("input[type=submit]", jqForm).attr("disabled", "disabled");
		$(".ajax-loader",jqForm).show();
		return true;
	}
	
	function onLoginError(XMLHttpRequest, textStatus, errorThrown){
		err = $("#login .err-msg");
		err.text("未知的错误: " + XMLHttpRequest.responseText);
		unlockAndHideLoginForm();
	}
	
	function unlockAndHideLoginForm(){
		form = $("#login form");
		$("input[type=submit]", form).removeAttr("disabled");
		$(".ajax-loader", form).hide();
	}
	
	function processLoginResponse(data){
		//在制定了一个json格式的前提下，再来实现这个函数。
		err = $("#login .err-msg");
		if(data.state == "success"){
			if($("#refer").size() > 0){
				newloc = $("#refer").val();
				if (newloc == ""){
					location = "/";
				}else{
					location = newloc;
				}
			}else{
				location.reload();
			}
			return false;
		}else if(data.state == 'fail'){
			if(data.data == undefined){
				err.text("登录失败，用户名或密码可能有误:)");
			}else{
				err.text(data.data);
			}
			err.css("display", "block");
		}else{
			err.text(data.toString());
		}
		unlockAndHideLoginForm();
		return false;
	}
	
	//窗口的关闭功能
	$(".close a").click(function(){
		$(this).parents(".to-close").hide();
		return false;
	});
	
	$(".j-stock-favor, .j-stock-unfavor").click(function(){
		/* 如果这个按钮是关注的按钮，那么向服务器端发送POST请求，成功的话，刷新页面，失败的话显示错误原因 */
		if($(this).attr("class").search(/stock-favor/) != -1){
			url = "/sec/favor";
		}else if($(this).attr("class").search(/stock-unfavor/) != -1){
			if(window.confirm("您真的不想再关注这个证券了吗？")){
				url = "/sec/unfavor";
			}else{
				return false;
			}
		}
		d = $(this).parent("div");
		followLoader = $(".follow-ajax-loader", d).show();
		i = $('input[name=id]',d).val();
		$.post(url, {id : i}, function(response){
			var json = eval('(' + response + ')');
			if (json == null){
				$(".follow-publish-err",d).text(response);
			}else if(json.state == 'fail'){
				$(".follow-publish-err",d).text(json.data);
			}else{
				location.reload();
			}
			followLoader.hide();
		});
		return false;

	});

	
	$('#say').focus(function(){
		$(this).css("height","5em").width("400px");
	}).blur(function(){
		if($(this).val().length == 0){
			$(this).css("height", "1.3em").width("200px");
		}
	});
	
	$('.char-limit-240').each(function(j){
		i = $(this);
		remain = 240 - i.val().length;
		submit = $("button[type=submit]");
		if (remain < 0){
			submit.attr("disabled", "disabled");
		}else{
			submit.removeAttr("disabled");
		}
		td = i.parent('td').get(0);
		$(".limit-num", td).text(remain);
	});
	
	$('.char-limit-240').keyup(function(){
		i = $(this);
		remain = 240 - i.val().length;
		submit = $("button[type=submit]");
		if (remain < 0){
			submit.attr("disabled", "disabled");
		}else{
			submit.removeAttr("disabled");
		}
		//alert(remain + $(this).val());
		td = i.parent('td').get(0);
		$(".limit-num", td).text(remain);
	});
	
	$('#register #username').keyup(function(){
		t = $(this).val();
		$('#register .url-name').text(t);
	});
	
	$('#register .url-name').text($('#register #username').val());
});
